import { defineStore } from 'pinia'
import { ALL_CATEGORY_ITEM, CATEGORY_NOMAR_DATA } from '@/constants'
import { getCategory } from '@/api/category'

export const useCategoryStore = defineStore('category', {
  state: () => ({
    categorys: CATEGORY_NOMAR_DATA
  }),
  getters: {},
  actions: {
    // 设置分类
    setCategorys(categorys) {
      this.categorys = [ALL_CATEGORY_ITEM, ...categorys]
    },
    async getCategory() {
      const { categorys = [] } = await getCategory()
      this.setCategorys(categorys)
    }
  },
  // 开启数据缓存
  // 方式一：
  persist: true
  // 方式二：
  // persist: {
  //   key: 'category',
  //   storage: window.localStorage,
  //   paths: ['categorys']
  // }
})
